package com.huangfu.learning.javabase.java.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/13
 * Time: 15:45
 * Version: 1.0
 * Description:获取其他类中的构造函数，取到的顺序与实际定义顺序相反，实例化其他的类
 **/
public class GetConstruct {

    public static void main(String[] args) {
        String a = "123";
        Class<?> clazz = null;
        try {
            clazz = Class.forName("com.huangfu.learning.javabase.java.reflect.Student");
            Constructor<?>[] constructors = clazz.getConstructors();
            for (Constructor constructor : constructors) {
                //取到的顺序与实际定义顺序相反
                System.out.println("构造方法：  " + constructor);
//                构造方法：  public com.huangfu.learning.javabase.java.reflect.Student(java.lang.String,int)
//                构造方法：  public com.huangfu.learning.javabase.java.reflect.Student(int)
//                构造方法：  public com.huangfu.learning.javabase.java.reflect.Student(java.lang.String)
//                构造方法：  public com.huangfu.learning.javabase.java.reflect.Student()

                // 获取参数类型列表
                Class<?> p[] = constructor.getParameterTypes();
                int mo = constructor.getModifiers();
                System.out.println("构造函数权限修饰符" + Modifier.toString(mo) + " ");
                System.out.println("构造函数名字：" + constructor.getName());
                for (int j = 0; j < p.length; ++j) {
                    System.out.println("构造函数参数类型：" + p[j].getName());
                    if (j < p.length - 1) {
                        System.out.println(",");
                    }
                }
                System.out.println("=====================================");
                Student student1 = null;
                Student student2 = null;
                Student student3 = null;
                Student student4 = null;

                // 这里获得的构造函数顺序与实际构造函数顺序相反
                Constructor<?>[] student = clazz.getConstructors();
                try {
                    // 调用构造函数
                    student1 = (Student) student[3].newInstance();
                    student2 = (Student) student[2].newInstance("zhang");
                    student3 = (Student) student[1].newInstance(33);
                    student4 = (Student) student[0].newInstance("zhang", 33);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("student1:" + student1);
                System.out.println("student2:" + student2);
                System.out.println("student3:" + student3);
                System.out.println("student4:" + student4);

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class Student {
    public Student() {

    }

    public Student(String name) {
        this.name = name;
    }

    public Student(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name:" + this.name + ";age:" + this.age;
    }
}
